import os
from subprocess import *

class KlikCompatKDE(object):

	def __init__(self, klik):
		self.klik = klik

	def init_enviroment(self, cmg):

		if "KDE" in cmg.recipe.needs:
			os.environ["KDE_FORK_SLAVES"] = "1"

			# KDE - kdeinit
			# if starting a kde app make sure kdeinit is running first
			if self.klik.xdg.get_desktop_enviroment != "KDE":

				if Popen(["which", "start_kdeinit_wrapper"], stdout=PIPE, stderr=open(os.devnull, "w")).communicate()[0].strip():
					if not Popen(["pidof", "-x", "kdeinit"], stdout=PIPE, stderr=open(os.devnull, "w")).communicate()[0].strip():
						call(["start_kdeinit_wrapper"])

				if not Popen(["pidof", "-x", "kdeinit"], stdout=PIPE, stderr=open(os.devnull, "w")).communicate()[0].strip():
					call(["start_kdeinit"])
		return
